package com.glodon.wps.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.glodon.wps.vo.TokenResultVO;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;


public class TokenUtils {
    
    private static final String APP_TOKEN_KEY = "APP_TOKEN_KEY";
    
    public static void setAppToken(TokenResultVO appToken) {
        RequestContextUtils.getRequest().getServletContext().setAttribute(APP_TOKEN_KEY, appToken);
    }
    
    public static TokenResultVO  getAppToken() {
        return (TokenResultVO )RequestContextUtils.getRequest().getServletContext().getAttribute(APP_TOKEN_KEY);
    }

    public static void setAppToken(TokenResultVO appToken, StringRedisTemplate redisTemplate) {
        String json = JSONUtil.beanToJson(appToken);
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(APP_TOKEN_KEY, json);
    }

    public static void setAppToken(TokenResultVO appToken, StringRedisTemplate redisTemplate, long expire) {
        String json = JSONUtil.beanToJson(appToken);
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(APP_TOKEN_KEY, json, expire);
    }

    public static TokenResultVO  getAppToken(StringRedisTemplate redisTemplate) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        String json = valueOperations.get(APP_TOKEN_KEY);
        return JSONUtil.jsonToBean(json, TokenResultVO.class);
    }
        
}
